// app.js


App({
  onLaunch() {
    // 小程序自动更新


    // 展示本地存储能力
    const logs = wx.getStorageSync('logs') || []
    logs.unshift(Date.now())
    wx.setStorageSync('logs', logs)
    this.height();
    // 登录
    wx.login({
      success: res => {
        // 发送 res.code 到后台换取 openId, sessionKey, unionId
      }
    })
  },
  onShow(){
console.log(1234)
  },
 
  height() {
    // 获取状态栏高度
    const {
      statusBarHeight
    } = wx.getSystemInfoSync();
    // 得到右上角菜单的位置尺寸
    const menuButtonObject = wx.getMenuButtonBoundingClientRect();
    const {
      top,
      height
    } = menuButtonObject;
    // 计算导航栏的高度
    // 此高度基于右上角菜单在导航栏位置垂直居中计算得到
    const navBarHeight = height + (top - statusBarHeight) * 2;
    // 计算状态栏与导航栏的总高度
    const statusNavBarHeight = statusBarHeight + navBarHeight + 10;
    this.globalData.navBarHeight = statusNavBarHeight;
    this.globalData.navTop = top;
  },
  https_request : function(url,param='',method ='POST',is_load=false,is_msg=true,that,pages){
   
    var promise = new Promise((resolve, reject) => {
       //init
       if(is_load){
        wx.showLoading({
          title: '加载中...',
          mask: true 
        })
       }
       var options;
       if(pages){ //不是底部导航 
         var currentPage = pages[pages.length - 1]    //获取当前页面的对象
         //console.log(currentPage);
         var options = currentPage.options;
       }
       if(param){
        this.extend(param,options)
         //console.log(options);
       }
       //网络请求
       wx.request({
          url: url,
          data: param,
          method: method,
          header: {
            'content-type': method == 'GET' ? 'application/json' : 'application/x-www-form-urlencoded',
          },
          dataType: 'json',
          success: function (res) {//服务器返回数据
           // console.log(res.data) //res.data后台请求的完整数据
             if (res.data.code == 1) { //res.data 为 后台返回数据，格式为{"data":{...}, "info":"成功", "status":1}, 后台规定：如果status为1,既是正确结果。可以根据自己业务逻辑来设定判断条件
                resolve( res.data);
                that.setData(res.data);
             } else {//返回错误提示信息
              resolve( res.data);
                //reject( res.data );
             }
           
             if(is_msg){
              wx.showToast({
                title:res.data.msg,
                icon: 'none',
                duration: 2500,
                mask:false
              })

             }

          },
          error: function (e) {
             reject('网络出错');
          },complete: info => {
            if(is_load){
             // wx.hideLoading();
            }
            
           // hasClick = false
          }
       })
    });
    return promise;
 },curl : function(url,param='',method ='POST',is_load=false,is_msg=true,that,pages){
   
  var promise = new Promise((resolve, reject) => {
     //init
     if(is_load){
      wx.showLoading({
        title: '加载中...',
        mask: true 
      })
     }
     var options;
     if(pages){ //不是底部导航 
       var currentPage = pages[pages.length - 1]    //获取当前页面的对象
       //console.log(currentPage);
       var options = currentPage.options;
     }
     if(param){
      this.extend(param,options)
       //console.log(options);
     }
     //网络请求
     wx.request({
        url: url,
        data: param,
        method: 'POST',
        header: {
          'content-type': 'application/x-www-form-urlencoded',
        },
        dataType: 'json',
        success: function (res) {//服务器返回数据
         // console.log(res.data) //res.data后台请求的完整数据
           if (res.data.code == 1) { //res.data 为 后台返回数据，格式为{"data":{...}, "info":"成功", "status":1}, 后台规定：如果status为1,既是正确结果。可以根据自己业务逻辑来设定判断条件
              resolve( res.data);  
           } else {//返回错误提示信息
            resolve( res.data);  
              //reject( res.data );
           }
        },
        error: function (e) {
           reject('网络出错');
        },complete: info => {
          if(is_load){
           // wx.hideLoading();
          }
          
         // hasClick = false
        }
     })
  });
  return promise;
},
 https_view:function(url,param,that,pages){
  
  var promise = new Promise((resolve, reject) => {
    var options = [];
    if(pages){ //不是底部导航 
      var currentPage = pages[pages.length - 1]    //获取当前页面的对象
      //console.log(currentPage);
      options = currentPage.options;
    }
    console.log(options)
    console.log(param)
    if(param){
      //opt = options.concat(param) 
      options = this.extend(param,options)
    }
    console.log(options)
    wx.request({
       url: url,
       data: options,
       method: 'POST',
       header: {
         'content-type': 'application/x-www-form-urlencoded',
       },
       dataType: 'json',
       success: function (res) { //服务器返回数据
        res.data =  res.data.replace(/\<img/gi, '<img style="width:100%;height:auto"')
        that.setData({abc:res.data});
        resolve( res.data);
       },
       error: function (e) {
          reject('网络出错');
       },complete: info => {
        // hasClick = false
       }
    })
 });
 return promise;
 },msg:function(msg = '',time = 1500,icon = 'none'){
  wx.showToast({
    title:msg,
    icon: icon,
    duration: time,
    mask:false
  })
},extend:function(data,dataExtend){
  var res={};
  for (var key in data) {
    res[key] = data[key];
  } 
  for (var key in dataExtend) {
    res[key] = dataExtend[key]; 
  } 
  return res;
},
  globalData: {
    userInfo: null,
    navBarHeight: null,
    navTop: null,
    token:'',
    domain: 'https://app.donoy.cn'
  }
})